package com.incms.web.modules.system.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.incms.web.common.util.Page;
import com.incms.web.modules.system.dao.SysMenuDao;
import com.incms.web.modules.system.entity.SysMenu;
import com.incms.web.modules.system.enums.MenuTypeEnum;

@Service("sysMenuService")
public class SysMenuService {
	
	@Resource(name = "sysMenuDao")
	private SysMenuDao sysMenuDao;

	/**
	 * 查询所有菜单
	 *@param userId 用户编号
	 *@param object
	 *@param object2
	 *@return
	 */
	@Transactional(readOnly = true)
	public List<SysMenu> findMenuBy(Long userId, Long pid, MenuTypeEnum type) {
		List<SysMenu> list = sysMenuDao.findMenuBy(userId,pid,type);
		for(SysMenu m : list){
			List<SysMenu> children = sysMenuDao.findChildren(m.getMenuId());
			m.setChildMenus(children);
		}
		return list;
	}

	@Transactional(readOnly = true)
	public Page<SysMenu> querySysMenuPage(SysMenu sysMenu, Page<SysMenu> page) {
		return sysMenuDao.queryForListPage("SysMenuMapper.querySysMenuPage", sysMenu, page);
	}

	@Transactional(readOnly = true)
	public SysMenu findById(Long userId) {
		return sysMenuDao.findOne("SysMenuMapper.findById",userId);
	}

	@Transactional
	public Long save(SysMenu sysMenu) {
		return sysMenuDao.save("SysMenuMapper.saveMenu", sysMenu);
	}

	@Transactional
	public Long update(SysMenu sysMenu) {
		return sysMenuDao.save("SysMenuMapper.updateMenu", sysMenu);
	}

	@Transactional
	public Long delete(Long userId) {
		return sysMenuDao.save("SysMenuMapper.deleteMenu", userId);
	}

	@Transactional(readOnly = true)
	public List<SysMenu> findAll(){
		return sysMenuDao.findList("SysMenuMapper.findAll", null);
	}
	
	@Transactional(readOnly = true)
	public List<SysMenu> findIds(String ids){
		return sysMenuDao.findList("SysMenuMapper.findIds", ids);
	}
}
